
﻿<?php
/**
 * View the groups' desired project listed by priority.  
 * The list is sorted after when the groups applied for the project (First come, first served).
 */
// Start the session handling system
session_start();

// Connect to the database
require_once ("../db.php");

// Only allow this for employee users
if (!isset($_SESSION['uid']) || ($_SESSION['type'] != 'employee'))
    die('Not logged in as an external user');

//Select relevant info from three tables, and sortes them.
$sql = "SELECT * FROM projectrequest, projects, projectgroups
    WHERE projectrequest.projectid = projects.id AND projectrequest.groupid = projectgroups.id
    ORDER BY projectrequest.date, projectgroups.id, projectrequest.priority";

$sth = $db->prepare($sql);
$sth->execute();
$projectrq = $sth->fetchAll();
viewAllGivenProjects($projectrq); //The groups that has bin assigned a project. 
                                    //Function is in the end of page.
?>                                 

<h1 style="margin-top: 40px;" class="evalProjects">Grupper som må tildeles prosjekt</h1>
<table id="grProjectList"> <!--Create table for the hole list of group request-->
    <tr> <!--Headings for the rows in table:-->
        <td>Navn på prosjekt:</td>
        <td>Status</td>
        <td>Tildel</td>
    </tr>
    <?php

     $lastGroup = NULL;             //To know when the 'foreach' runs into a new group
     $groupFlag = NULL;             //Used to check if the group already has been assigned a project
     foreach ($projectrq as $row) { //The main loop for listing the group reguests
        foreach ($projectrq as $i) {//Checks if the group already has been assigned a project:
            if (($row['groupid'] == $i['groupid']) && ($i['priority'] == 'taken')) {
                $groupFlag = $row['groupid']; //If so, the flag is set.
            } //End if
        }  //End foreach    
                //If the priority isn't 'taken', and the flag is NULL
                                //this is a new group:
        if (($row['priority'] == 'high' || $row['priority'] == 'medium' ||
                $row['priority'] == 'low') && ($row['groupid'] != $groupFlag)) { 
            
            /********Group header*******
             * 
             * If this is the first project request to this group, 
             * make the header for this group:
             */
            if ($lastGroup != $row['groupid']) { 
                if ($lastGroup) {   //If first time: do not insert this space.
                                    //To make space between each group:
                    echo '<tr><td style="height: 20px;"></td></tr>'; 
                } //End if
                /*
                 * Sets the flag, to indicate which group it has startet on. 
                 * To ensure there will be only one header for each group:
                 */
                $lastGroup = $row['groupid']; 
                echo '<tr>';
                echo '<th class="studentlist" style="width: 400px;
                    background-color: #f0f0f0; color: rgb(51,51,51);">
                    <b>' . $row['name'] . '</b></th>'; //Groupname
                echo '</tr>';
            }//End if
            /********Project link*******/
            echo '<tr id="'.$row['groupid'].'" >'; //Id is used to assign the group a project.
            //displayOneApprovedProject is in /higface/requests.js
            $notes = 0; $status=true; $CComments = true;
            echo '<td class="studentlist" id="'.$row['name'].'" >
                    <a href="javascript: ;" onclick="displayOneApprovedProject
                    ('.$row[4].','.$notes.','.$status.','.$CComments.');" ><span id="proNameDialog">'.$row['title'].'</span></a>

                  </td>'; //The project request.
                                                                        
             /*******Status and assign icon*****/                                                           
            $flag = false; //Sets a flag to check if group has a projeckt.
            foreach ($projectrq as $i) { //The test if the project is taken by someone else:
                if (($row['projectid'] == $i['projectid']) && ($i['priority'] == 'taken')) {
                    $flag = true;
                } //End if
            } //End foreach
                /**If project is taken by someone**/
            if ($flag) { //If 'taken' write taken in the priority section:
                echo '<td style="color: #330066" class="studentlist">taken</td>';
                 //Button to assign project to group, 
                 //but since 'taken' by someone else, alert that:
                echo '<td class ="studentlist" id="'.$row['projectid'].'" >
                    <img id="assignIco" height="20" src="LnF/thumb_up.png" 
                    onclick="alert(\'Dette prosjektet er gitt til noen andre!\')"; /></td>';
            } //End if
            /**If project is available**/
            else { //If it issent; write the group's priority:
                echo '<td style="color: #330066" class="studentlist">' . $row['priority'] . '</td>';
                //Makes the image button used to assign the group that project:
                echo '<td class ="studentlist" id="'.$row['projectid'].'" >
                    <img id="assignIco" height="20" src="LnF/thumb_up.png" 
                    onclick="assignDialog(this);" /></td>'; //Is in /higface/employee/assignDialog.js
            } //End else      
            echo "</tr>";
        }//End main if
    }//End foreach
    ?>
</table> <!--End of main-table-->


<?php
/*******View assigned project******/
function viewAllGivenProjects($projectrq) { ?>
        <!--Button to view assigned project-->
    <button id="viewDoneProjects">Vis alle utdelte prosjekter</button>
    <div class="doneProjects" style="display: none">
        <h1 class="evaluatedProjects">Utdelte prosjekter</h1>
        <table id="projectList">
            <th style="text-align: left">Gruppenavn</th>
            <th style="text-align: left">Navn på prosjekt:</th>
            <?php //If project is assigned to group:
            foreach ($projectrq as $row) { 
                if ($row['priority'] == 'taken') {
                    echo "<tr>";
                    //Display group:
                    echo '<td style="color: #330066; width : 200px;" class="studentlist">' . $row['name'] . '</td>';
                    //Mae project link:
                    echo '<td style="width: 240px;"  class="studentlist"><a href="#" onclick="displayOneApprovedProject
                        ('.$row[4].','.$notes.','.$status.','.$CComments.');">' . $row['title'] . '</a></td>';
                    echo "</tr>";
                }//end if
            }//End foreach
            ?>
        </table>
    </div>
<?php 
}
//?>
<script type="text/javascript">
    //Hide and view effect for assigned projects:
    $("#viewDoneProjects").click(function(){ 
        $(".doneProjects").toggle("slow");
    });
</script>
